
cd "C:\Users\14258\Dropbox\Projects\Rural Economic Ideology\Analysis"
use ANES2020.dta, clear


********* SURVEY WEIGHT VARIABLES **********

gen weight = .
replace weight = V200010b
drop if weight == . // Remove Those Who Didn't Complete Post-Election

gen strata = .
replace strata = V200010d

gen psu = .
replace psu = V200010c

svyset [pweight = weight], psu(psu) strata(strata)

* Create Frequency Weight for Histograms - DO NOT USE FOR ANYTHING ELSE, SEs will be misleading!
local k = 2  // Match sampling weights to k = 2 decimal places: e.g. sampling weight = 3.212 -> fwt = 321 
gen fwt = round(10^(`k')*weight,1)


********* Rural Classification and Subsetting **********

* Rural/Small-Town Identifiers
gen rural_person = .
replace rural_person = 1 if V202356 == 4 | V202356 == 3
replace rural_person = 0 if V202356 == 1 | V202356 == 2 

gen smalltown = 1 if V202356 == 3
replace smalltown = 0 if V202356 == 4

drop if rural_person !=1 // Remove Non-Rural/Non-Small Town Folks

* Rural/Small-Town Residents
gen rural_location = .
replace rural_location = 1 if V202355 == 1 | V202355 == 2
replace rural_location = 0 if V202355 == 3 | V202355 == 4

* Note: Do not remove rural identifiers who aren't rural residents, but do robustness check in Appendix 4A.


************ DEMOGRAPHICS **************

* Gender
gen male = .
replace male = 1 if V201600 == 1
replace male = 0 if V201600 == 2 | V201600 == -9

* Race/Ethnicity
gen race = .
replace race = 1 if V201549x == 1
replace race = 2 if V201549x == 2
replace race = 3 if V201549x == 3
replace race = 4 if V201549x == 4
replace race = 5 if V201549x == 5
replace race = 6 if V201549x == 6 | V201549x < 0
label define racelabel 1 "White" 2 "Black" 3 "Hispanic" 4 "Asian/PI" 5 "Native" 6 "Multi"
label values race racelabel

gen white = .
replace white = 1 if race == 1
replace white = 0 if race != 1 & race != .
gen black = .
replace black = 1 if race == 2
replace black = 0 if race != 2 & race != .
gen hispanic = .
replace hispanic = 1 if race == 3
replace hispanic = 0 if race != 3 & race != .
gen asian = .
replace asian = 1 if race == 4
replace asian = 0 if race != 4 & race != .
gen native = .
replace native = 1 if race == 5
replace native = 0 if race != 5 & race != . 
gen otherrace = .
replace otherrace = 1 if race == 6
replace otherrace = 0 if race != 6 & race != . 

gen nonwhite = 1-white

* Education
gen education = .
replace education = (V201510-1)/7 if V201510 > 0 & V201510 < 9

* Parent
gen parent = .
replace parent = 1 if V201567 == 1 | V201567 == 2 | V201567 == 3 | V201567 == 4
replace parent = 0 if V201567 == 0

* Married
gen married = 0 if V201508 > 0
replace married = 1 if V201508 == 1 | V201508 == 2

* Union
gen union = 0 if V201544 == 2
replace union = 1 if V201544 == 1

* Unemployed
gen unemployed = 0 if V201534x > 0 
replace unemployed = 1 if V201534x == 2 | V201534x == 4

* Age
gen age = .
replace age = (V201507x-18)/62 if V201507x > 17

* Region
gen south = .
replace south = 1 if V203003 == 3
replace south = 0 if V203003 != 3
gen northeast = 0
replace northeast = 1 if V203003 == 1
gen midwest = 0 
replace midwest = 1 if V203003 == 2
gen west = 0
replace west = 1 if V203003 == 4

* State
gen state = V203000

* Income
gen income = .
replace income = (V201617x-1)/21 if V201617x > 0
gen income_rev = 1-income 

* Religiosity
gen religion = 1-(V201433-1)/4 if V201433 > 0 

* Church Attendance
gen church = 0 if V201452 == 2
replace church = 1-(V201453-1)/4 if V201453 > 0

* Old White Southerners (for Ancestral White Southern Democrat Robustness Check)
gen ancestral = 0 if V201507x > 0
replace ancestral = 1 if V201507x >= 55 & south == 1 & white == 1


*********** PARTY AND IDEOLOGY *************

* 7-pt Partisanship
gen pid01 = .
replace pid01 = 1-(V201231x-1)/6 if V201231x > 0

gen democrat = 0
replace democrat = 1 if V201231x == 1 | V201231x == 2 | V201231x == 3
gen republican = 0
replace republican = 1 if V201231x == 5 | V201231x == 6 | V201231x == 7
gen independent = 0
replace independent = 1 if pid01 == .5

* Partisan Identity Strength
gen pidstrength = (abs(pid01-.5))*2

* Partisan Identity Importance
gen pid_importance = .
replace pid_importance = 1-(V201232-1)/4 if V201232 > 0 

* Ideological Self-Placement
gen ideo01 = .
replace ideo01 = 1-(V201200-1)/6 if V201200 > 0 & V201200 < 10
replace ideo01 = .5 if V201200 == 99 // Note: Appendix Table 2A shows relationships of rural consc*engagement are similar for "Moderate" and "99- Haven't thought much about this". 

gen liberal = 0
replace liberal = 1 if V201200 == 1 | V201200 == 2 | V201200 == 3
gen conservative = 0
replace conservative = 1 if V201200 == 5 | V201200 == 6 | V201200 == 7
gen moderate = 0
replace moderate = 1 if V201200 == 4 | V201200 == 9

* Party Feeling Thermometers
gen ft_dem = V201156/100 if V201156 >= 0 & V201156 <= 100
gen ft_rep = V201157/100 if V201157 >= 0 & V201157 <= 100
gen ft_demrep = ft_dem-ft_rep
replace ft_demrep = (ft_demrep+1)/2

* Ideological Group Feeling Thermometers
gen ft_lib = V202161/100 if V202161 >= 0 & V202161 <= 100
gen ft_con = V202164/100 if V202164 >= 0 & V202164 <= 100
gen ft_libcon = ft_lib-ft_con
replace ft_libcon = (ft_libcon+1)/2

* Biden/Trump Feeling Thermometers
gen ft_biden = V201151/100 if V201151 >= 0 & V201151 <= 100
gen ft_trump = V201152/100 if V201152 >= 0 & V201152 <= 100
gen ft_bidentrump = ft_biden-ft_trump
replace ft_bidentrump = (ft_bidentrump+1)/2

* Trump Approval (Note: Follows Nelsen and Petsko Operationalization)
gen trump_approval_gen = .
replace trump_approval_gen = 1-(V201129x-1)/3 if V201129x > 0 
gen trump_approval_econ = .
replace trump_approval_econ = 1-(V201132x-1)/3 if V201132x > 0 
gen trump_approval_fp = .
replace trump_approval_fp = 1-(V201135x-1)/3 if V201135x > 0 
gen trump_approval_hlth = .
replace trump_approval_hlth = 1-(V201138x-1)/3 if V201138x > 0 
gen trump_approval_imm = .
replace trump_approval_imm = 1-(V201141x-1)/3 if V201141x > 0
gen trump_approval_cov = .
replace trump_approval_cov = 1-(V201144x-1)/3 if V201144x > 0 
alpha trump_approval_gen trump_approval_econ trump_approval_fp trump_approval_hlth trump_approval_imm trump_approval_cov, gen(trump_approval)
gen trump_disapproval=1-trump_approval

* Presidential Turnout/Vote Choice/Preference
gen voted2020 = .
replace voted2020 = 1 if V202068x == 2
replace voted2020 = 0 if V202068x == 0 | V202068x == 1

gen presvote2020 = .
replace presvote2020 = 0 if voted2020 == 0 | V202072 == 2
replace presvote2020 = 1 if V202073 == 1
replace presvote2020 = 2 if V202073 == 2
replace presvote2020 = 3 if V202073 > 2

label define presvote2020l 0 "Non-Voter" 1 "Biden" 2 "Trump" 3 "Other Candidate"
label values presvote2020 presvote2020l

gen twoparty2020 = presvote2020 if presvote2020 == 1 | presvote2020 == 2
label define twopartyl 1 "Biden" 2 "Trump"
label values twoparty2020 twopartyl

gen biden2020 = 1 if twoparty2020 == 1
replace biden2020 = 0 if twoparty2020 == 2

gen nonvoter_2020pref = .
replace nonvoter_2020pref = 1 if V202079x == 10 | V202079x == 11
replace nonvoter_2020pref = 2 if V202079x == 20 | V202079x == 21
label values nonvoter_2020pref twopartyl 

gen candpref2020 = nonvoter_2020pref if twoparty2020 == . 
replace candpref2020 = twoparty2020 if nonvoter_2020pref == .
replace candpref2020 = 1-(candpref2020-1)

* Two-Way Margins for Voters and Non-Voters (Conclusion Analysis)
svy: tab twoparty2020 // +24 Trump Among Voters
svy: tab nonvoter_2020pref // +4 Trump Among Non-Voters

* Construct 7-Item Partisan-Ideological Identification Scale (Appendix Table 2C)
factor pid01 ft_demrep candpref2020 ft_bidentrump ideo01 ft_libcon trump_disapproval, pcf
alpha pid01 ft_demrep candpref2020 ft_bidentrump ideo01 ft_libcon trump_disapproval, gen(latent_leftright)

* Notes: A single factor accounts for ~80% of shared variance across the seven items (loadings = .81–.95), confirming a unidimensional left–right orientation.
* Slightly lower ideology loadings likely reflect that factor models weight each item's contribution by shared covariance structure;
* with five partisan and only two ideology measures, the "partisan" covariance dominates the factor, attenuating the apparent loadings of the ideology items.
* Given theoretical reason to treat these dimensions equivalently—and that factor loadings are high and approximately uniform—an additive scale is more appropriate than using factor scores.


************ Rural Consciousness **************

* Rural Consciousness
gen ruralconsc1 = .
replace ruralconsc1 = (V202276x-1)/6 if V202276x > 0 
gen ruralconsc2 = .
replace ruralconsc2 = (V202279x-1)/6 if V202279x > 0 
gen ruralconsc3 = .
replace ruralconsc3 = (V202282x-1)/6 if V202282x > 0 

alpha ruralconsc1 ruralconsc2 ruralconsc3, gen(ruralconsc)

gen ruralconsc3cat = .
replace ruralconsc3cat = 1 if ruralconsc > .5 & ruralconsc <= 1
replace ruralconsc3cat = 2 if ruralconsc == .5
replace ruralconsc3cat = 3 if ruralconsc < .5 & ruralconsc >= 0

gen ruralconsc_midup = .
replace ruralconsc_midup = round((ruralconsc-0.5)*2,.00001) if ruralconsc >= .49

gen ruralconsc_anti = .
replace ruralconsc_anti = 1 if ruralconsc < .5
replace ruralconsc_anti = 0 if ruralconsc >= .5

* Facets
gen ruralconsc_economic = ruralconsc1
gen ruralconsc_political = ruralconsc2
gen ruralconsc_cultural = ruralconsc3


******** RACIAL ATTITUDES **********

* Black-White Stereotyping Battery
gen stereo_white_hard = .
replace stereo_white_hard = 1-(V202515-1)/6 if V202515 > 0 
gen stereo_black_hard = .
replace stereo_black_hard = 1-(V202516-1)/6 if V202516 > 0 
gen stereo_wb_hard = ((stereo_white_hard-stereo_black_hard)+1)/2

gen stereo_white_peace = .
replace stereo_white_peace = 1-(V202521-1)/6 if V202521 > 0 
gen stereo_black_peace = .
replace stereo_black_peace = 1-(V202522-1)/6 if V202522 > 0 
gen stereo_wb_peace = ((stereo_white_peace-stereo_black_peace)+1)/2

alpha stereo_wb_peace stereo_wb_hard, gen(antiblack_stereotyping)

* Racial Resentment
gen raceres1=1-((V202300-1)/4) if V202300>0
gen raceres2=(V202301-1)/4 if V202301>0
gen raceres3=(V202302-1)/4 if V202302>0
gen raceres4=1-((V202303-1)/4) if V202303>0
alpha raceres1 raceres2 raceres3 raceres4, gen(raceres)


************ POLITICAL ENGAGEMENT **************

* Political Interest
gen attention = .
replace attention = 1-(V201005-1)/4 if V201005 > 0
gen campaign = .
replace campaign = 1-(V201006-1)/2 if V201006 > 0
gen interest = .
replace interest = 1-(V202406-1)/3 if V202406 > 0
gen news = .
replace news = 1-(V202407-1)/3 if V202407 >0
replace V201629a = . if V201629a < 0
replace V201629b = . if V201629b < 0
replace V201629c = . if V201629c < 0
replace V201629d = . if V201629d < 0
egen followrace = rmean(V201629a-V201629d)

alpha attention campaign interest news followrace, generate(polinterest)

* Political Knowledge
gen know1 = .
replace know1 = 1 if V201644 == 6
replace know1 = 0 if V201644 != 6
gen know2 = .
replace know2 = 1 if V201646 == 1
replace know2 = 0 if V201646 != 1
gen know3 = .
replace know3 = 1 if V201647 == 2
replace know3 = 0 if V201647 != 2
gen know4 = .
replace know4 = 1 if V202138y == 1
replace know4 = 0 if V202138y == 0 | V202138y == -9
gen know5 = .
replace know5 = 1 if V202139y1 == 1
replace know5 = 0 if V202139y1 == 0 | V202139y1 == -9
gen know6 = .
replace know6 = 1 if V202140y1 == 1
replace know6 = 0 if V202140y1 == 0 | V202140y1 == -9
gen know7 = .
replace know7 = 1 if V202141y1 == 1
replace know7 = 0 if V202141y1 == 0 | V202141y1 == -9
gen know8 = .
replace know8 = 1 if V202142y2 == 1
replace know8 = 0 if V202142y2 == 0 | V202142y2 == -9

alpha know1-know8, generate(knowledge)

* Political Engagement
alpha polinterest knowledge, generate(engagement)
gen engagement_rev = 1-engagement

gen engagement3 = 1 if engagement >= 0 & engagement <= .33
replace engagement3 = 2 if engagement > .33 & engagement <= .67
replace engagement3 = 3 if engagement > .67 & engagement <= 1

gen engagement4 = 1 if engagement >= 0 & engagement <= .25
replace engagement4 = 2 if engagement > .25 & engagement <= .50
replace engagement4 = 3 if engagement > .50 & engagement <= .75
replace engagement4 = 4 if engagement > .75 & engagement <= 1


corr ruralconsc engagement [aweight=weight] // Note: r=0.10 correlation


********* Material Interest Indicators **********

* Household Member Unemployed?
gen unemployed_household = unemployed
replace unemployed_household = 1 if V201542 == 2 | V201542 == 4

* General Self-Rated Health
gen poorhealth = .
replace poorhealth = (V201623-1)/4 if V201623 > 0 

* Stock Owner
gen ownstock = .
replace ownstock = 1 if V201606 == 1
replace ownstock = 0 if V201606 == 2
gen ownstock_no=1-ownstock

* Social Class
gen class = .
replace class = 1 if V202352 == 1
replace class = 2 if V202352 == 2
replace class = 3 if V202353 == 1
replace class = 4 if V202353 == 2
replace class = 5 if V202353 == 3
replace class = 6 if V202352 == 4
replace class = (class-1)/5
gen lowclass=1-class

* Income is already coded in the demographics block


******** POLITICAL PARTICIPATION **********

* Primary Vote Turnout
gen primary_vote = .
replace primary_vote = 1 if V201020 == 1
replace primary_vote = 0 if V201020 == 2

* Care about 2020 Election Outcome
gen care2020outcome = .
replace care2020outcome = 1-(V201216-1)/4 if  V201216 > 0 

* Attends Political Events
gen polevent_attend = .
replace polevent_attend = 1 if V202013 == 1 | V202014 == 1
replace polevent_attend = 0 if V202013 == 2 & V202014 == 2

* Has Campaign Swag
gen campaignswag = .
replace campaignswag = 1 if V202015 == 1
replace campaignswag = 0 if V202015 == 2

* Was Campaign Worker/Volunteer
gen campaignworker = .
replace campaignworker = 1 if  V202016 == 1
replace campaignworker = 0 if  V202016 == 2

* Made Political Donations
gen donor = .
replace donor = 1 if  V202017 == 1 |  V202019 == 1 |  V202021 == 1
replace donor = 0 if  V202017 == 2 &  V202019 == 2 &  V202021 == 2

* Engaged in Political Discussion
gen poldiscussion = .
replace poldiscussion = 1 if V202024 == 1
replace poldiscussion = 0 if V202024 == 2

* Attended Protests
gen protest_attend = .
replace protest_attend = 1 if V202025 == 1
replace protest_attend = 0 if V202025 == 2

* Signed Petitions
gen petition = .
replace petition = 1 if V202026 == 1
replace petition = 0 if V202026 == 2

* Posted to Social Media about Politics
gen polposting = .
replace polposting = 1 if V202029 == 1
replace polposting = 0 if V202029 == 2

* Contact Elected Official
gen member_contact = .
replace member_contact = 1 if V202030 == 1
replace member_contact = 0 if V202030 == 2

* Contact Federal Official
gen fedgov_contact = .
replace fedgov_contact = 1 if V202034 == 1
replace fedgov_contact = 0 if V202034 == 2

* Contact State Official
gen stategov_contact = .
replace stategov_contact = 1 if V202024 == 1
replace stategov_contact = 0 if V202024 == 2

* Contacts Elected Officials (Scale)
egen contacts = rmean(stategov_contact fedgov_contact member_contact)

* Attends Community Meetings
gen community_meeting = .
replace community_meeting = 1 if V202032 == 1
replace community_meeting = 0 if V202032 == 2

pwcorr engagement voted2020 primary_vote care2020outcome polevent_attend campaignswag campaignworker donor poldiscussion protest_attend petition polposting contacts member_contact fedgov_contact stategov_contact community_meeting [aweight=weight], star(0.05) // Note: Engaged rural Americans have more non-visible and visible political participation than disengaged rural Americans. 


******** ECONOMIC POLICY INDEX **********

* Obamacare
gen obamacare = .
replace obamacare = 1-(V202328x-1)/6 if V202328x > 0 

* Public vs. Private Health Insurance
gen govt_insurance = .
replace govt_insurance = 1-(V201252-1)/6 if V201252 > 0 
replace govt_insurance = .5 if V201252 == 99

* Government Spending on Healthcare
gen health_spending = .
replace health_spending = 1-(V202380x-1)/6 if V202380x > 0 

* Spending/Services vs. Taxes Trade-Off
gen spendingservices = .
replace spendingservices = (V201246-1)/6 if V201246 > 0 
replace spendingservices = .5 if V201246 == 99

* Support for Federal Job/Income Guarantee
gen jobguarantee = .
replace jobguarantee = 1-(V201255-1)/6 if V201255 > 0 
replace jobguarantee = .5 if V201255 == 99

* Government Spending on Social Security
gen ss_spending = .
replace ss_spending = 1-(V201302x-1)/4 if V201302x > 0

* Government Spending on Public Education
gen schools_spending = .
replace schools_spending = 1-(V201305x-1)/4 if V201305x > 0

* Government Spending on Assistance to Poor
gen poor_spending = .
replace poor_spending = 1-(V201320x-1)/4 if V201320x > 0

* Government Spending on Welfare
gen welfare_spending = .
replace welfare_spending = 1-(V201314x-1)/4 if V201314x > 0

* Support for Taxing Incomes over $1M
gen millionaire_tax = .
replace millionaire_tax = 1 if V202325 == 1 
replace millionaire_tax = .5 if V202325 == 3
replace millionaire_tax = 0 if V202325 == 2 

* Support for Govt Interventions to Reduce Income Inequality
gen inequality_reduction = .
replace inequality_reduction = 1-(V202259x-1)/6 if V202259x > 0 

* Minimum Wage 
gen minwage = .
replace minwage = 1-(V202377-1)/3 if V202377 > 0 

* Universal Basic Income
gen ubi = .
replace ubi = 1-(V202376x-1)/6 if V202376x > 0 

* Support for Regulations/Deregulation
gen regulations = .
replace regulations = 1-(V202256-1)/6 if V202256 > 0 

* Support for Import Restrictions
gen imports = .
replace imports = 1-(V202231x-1)/3 if V202231x > 0 

* Opposition to Free Trade
gen freetrade = .
replace freetrade = (V202361x-1)/6 if V202361x > 0 
alpha imports freetrade, gen(trade_scale)

* Construct Economic Policy Ideology Scale, Omit Trade Items (Appendix Tables 2D and 2E)
factor obamacare govt_insurance health_spending spendingservices jobguarantee poor_spending schools_spending ss_spending welfare_spending millionaire_tax inequality_reduction minwage regulations ubi freetrade imports, pcf

* Notes: Trade items load weakly negatively (despite being coded in same direction with respect to economic principles, i.e., where protectionism = left-wing, free trade = right-wing).
* Theoretical and empirical reasons to evaluate trade policy preferences separately from other economic policy preferences.
* Social security spending loads weakly onto first factor, but no theoretical reason to exclude from economic policy preferences. 

factor obamacare govt_insurance health_spending spendingservices jobguarantee poor_spending schools_spending ss_spending welfare_spending millionaire_tax inequality_reduction minwage regulations ubi, pcf 
alpha obamacare govt_insurance health_spending spendingservices jobguarantee poor_spending schools_spending ss_spending welfare_spending millionaire_tax inequality_reduction minwage regulations ubi, gen(econ_liberalism)
gen econ_conservatism = 1-econ_liberalism

* Notes: Omitting trade items increases proportion explained by first factor considerably.
* Social security spending loads weakly onto first factor, but no theoretical reason to exclude from general economic policy preferences. 
* All other items load well onto first factor -- second factor narrowly exceeds Eigenvalue of 1, but basically is just social security spending. 
* Analyzing social security, conditional relationships are ~ similar, if attenauted. Suggests inclusion in  economic policy ideology scale is OK. 

alpha obamacare govt_insurance health_spending, gen(healthcare) 
alpha poor_spending welfare_spending inequality_reduction, gen(redistribution) // Note: Items explicitly involve means-tested redistribution


* Save Dataset
save ANES2020_Coded.dta, replace


